package com.indusfo.spc.service.impl;

import com.indusfo.spc.exception.GlobalException;
import com.indusfo.spc.exception.ModifyFailedException;
import com.indusfo.spc.exception.ParamsErrorException;
import com.indusfo.spc.mapper.ProcessOfflineListMapper;
import com.indusfo.spc.pojo.ProcessOfflineList;
import com.indusfo.spc.service.ProcessOfflineListService;
import com.indusfo.spc.vo.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ProjectName: jpa05
 * @Package: com.indusfo.spc.service.impl
 * @ClassName: ProcessOfflineListServiceImpl
 * @Author: 熊冰
 * @Description:
 * @Date: 2019/10/28 15:04
 * @Version: 1.0
 */
@Service
public class ProcessOfflineListServiceImpl implements ProcessOfflineListService {
	// 声明日志的静态变量
	private static final Logger logger = LoggerFactory.getLogger(ProcessOfflineListServiceImpl.class);

	@Resource
	private ProcessOfflineListMapper processOfflineListMapper;

	/**
	 * 分页查询 pagesize pageindex
	 * 模糊查询 dim
	 * ID查询  Pid
	 * 返回总数 count
	 * @param processOfflineList
	 * @return
	 */
	@Override
	public JSONObject queryProcessOfflineList(ProcessOfflineList processOfflineList) {
		try {
			//获取页面的数据个数
			Integer pagesize = processOfflineList.getPagesize();
			//获取查询的页数
			Integer pageindex = processOfflineList.getPageindex();
			if(pagesize!=null && pageindex !=null){
				processOfflineList.setIncept(pagesize*(pageindex-1));
			}
			List<ProcessOfflineList> listProcessOfflineList=processOfflineListMapper.queryProcessOfflineList(processOfflineList);
			if(listProcessOfflineList.isEmpty()){
				return  JSONObject.oK("没有查询到相关消息",listProcessOfflineList,0);
			}
			//如果页数和页面数量都不为空的情况下进行计数

			Integer count= processOfflineListMapper.countProcessOfflineList(processOfflineList);
			return JSONObject.oK("查询成功",listProcessOfflineList,count);
		} catch (GlobalException e) {
			// 捕获异常,打印并返回数
			logger.error(e.getMessage(), e);
			return JSONObject.build(500, e.getMessage());

		}
	}

	/**
	 * 非空判断 长度验证   checkParam
	 * 查重判断 checkParamsNotRepeat   问题这个不需要查重吧?
	 * 默认值赋值 setDataState(1)
	 * @param processOfflineList
	 * @return
	 */
	@Override
	public JSONObject insertProcessOfflineList(ProcessOfflineList processOfflineList) {
		/**
		 * 这里还要判断哪些不为空
		 */
		try{
			if(processOfflineList.getProcessOfflineListId()!=null){
				throw new ModifyFailedException("新增是Id不能存在");
			}
			if(processOfflineList.getDataState()==null){
				processOfflineList.setDataState(1);
			}
			checkParam(processOfflineList);

			processOfflineList.setDataState(1);
			Integer row=processOfflineListMapper.insertProcessOfflineList(processOfflineList);
			if(row==0){
				throw new ModifyFailedException("新增不良现象失败");
			}
			return JSONObject.oK("新增成功");
		}catch(GlobalException e) {//捕获自定义异常
			logger.error(e.getMessage());
			return JSONObject.build(500, e.getMessage());
		}
	}

	/*多数据删除 Long[] bldcIds
	级联删除 delteBldcByBldcTypeId*/
	@Override
	public JSONObject deleteProcessOfflineList(List<Integer> processOfflineListIds, Integer IDataState) {
		try{
			//判断传入的参数是否为null
			if(IDataState == null) {
				//为null抛出异常
				throw new ParamsErrorException("数据状态不能为空");
			}else if(IDataState != 1 && IDataState != 2 && IDataState != 3) {
				throw new ParamsErrorException("数据状态错误");
			}
			if(processOfflineListIds == null) {
				throw new ParamsErrorException("请选择设备资料");
			}
			//调用删除的接口
			int row = processOfflineListMapper.deleteProcessOfflineList(processOfflineListIds,IDataState);
			//声明msg字符串，用来存抛出的异常
			String msg = "";
			if (row == 0) {
				// 判断传入的数据状态参数lDataState,返回相应信息
				switch (IDataState) {
					case 1:
						throw new ModifyFailedException("启用失败");
					case 2:
						throw new ModifyFailedException("删除失败");
					case 3:
						throw new ModifyFailedException("停用失败");
					default:
						break;
				}
			} else {
				switch (IDataState) {
					case 1:
						msg = "启用成功";break;
					case 2:
						msg = "删除成功";break;
					case 3:
						msg = "停用成功";break;
					default:
						break;
				}
			}
			return JSONObject.oK(msg);
		}catch(GlobalException e) {
			logger.error(e.getMessage());
			return JSONObject.build(500, e.getMessage());
		}
	}

	/**
	 * 非空判断 checkParam
	 * 查重判断 checkParamsNotRepeat
	 * 默认值赋值 setDataState(1)
	 * @param processOfflineList
	 * @return
	 */
	@Override
	public JSONObject updateProcessOfflineList(ProcessOfflineList processOfflineList) {
		try {
			//判断传入的参数是否为null
			if(processOfflineList.getProcessOfflineListId()==null) {
				//为null则抛出异常
				throw new ParamsErrorException("修改不良现象id不能为空");
			}
			if(processOfflineList.getDataState()==null){
				processOfflineList.setDataState(1);
			}
			checkParam(processOfflineList);

			//调用修改的接口
			int row = processOfflineListMapper.updateProcessOfflineList(processOfflineList);
			if(row == 0) {
				throw new ModifyFailedException("数据修改失败");
			}
			return JSONObject.oK("数据修改成功");
		}catch (GlobalException e) {//捕获自定义异常
			logger.error(e.getMessage());
			return JSONObject.build(500, e.getMessage());
		}
	}
	public void checkParam(ProcessOfflineList processOfflineList){
		if(processOfflineList.getOfflineId()==null){
			throw new ParamsErrorException("维修ID不能为空");
		}

		if(processOfflineList.getOfflineListId()==null){
			throw new ParamsErrorException("维修子工序ID不能为空");
		}
		if(processOfflineList.getProVerId()==null){
			throw new ParamsErrorException("版本ID不能为空");
		}
		if(processOfflineList.getDeteProjId()==null){
			throw new ParamsErrorException("采集项目ID不能为空");
		}

		//规格上限
		String vcUpperLimit = processOfflineList.getUpperLimit();
		//规格下限
		String vcLowerLimit = processOfflineList.getLowerLimit();
		//中心值
		String vcCenter = processOfflineList.getCenter();
		//内控上限
		String vcUpperIn = processOfflineList.getUpperIn();
		//内控下限
		String vcLowerIn = processOfflineList.getLowerIn();

		if (StringUtils.isEmpty(vcUpperLimit)) {
			throw new ParamsErrorException("请填写规格上限");
		}
		if (StringUtils.isEmpty(vcLowerLimit)) {
			throw new ParamsErrorException("请填写规格下限");
		}
		if (StringUtils.isEmpty(vcCenter)) {
			throw new ParamsErrorException("请填写规格中心值");
		}
		if (vcUpperLimit.getBytes().length > 50) {
			throw new ParamsErrorException("规格上限超过规定长度");
		}
		if (vcLowerLimit.getBytes().length > 50) {
			throw new ParamsErrorException("规格下限超过规定长度");
		}
		if (vcCenter.getBytes().length > 50) {
			throw new ParamsErrorException("规格中心值超过规定长度");
		}
		//上下限数值判断
		if (Double.parseDouble(vcUpperLimit) <= Double.parseDouble(vcLowerLimit)) {
			throw new ParamsErrorException("规格上限值小于规格下限值");
		}
		if (Double.parseDouble(vcCenter) < Double.parseDouble(vcLowerLimit)) {
			throw new ParamsErrorException("规格中心值小于规格下限值");
		}
		if (Double.parseDouble(vcCenter) > Double.parseDouble(vcUpperLimit)) {
			throw new ParamsErrorException("规格中心值大于规格上限值");
		}

		// 名称不能重复
		Integer Counts = processOfflineListMapper.checkProcessOfflineList(processOfflineList);
		if (Counts !=0) {
			throw new ParamsErrorException("该物料已存在");
		}

	}
}
